package 宝贝复制;

import LuceneIndexCreate.DB;
import LuceneIndexCreate.NumiidGet;
import LuceneIndexCreate.model.CatData;
import LuceneIndexCreate.model.CatIdsFrom1688;
import com.alibaba.fastjson.JSON;
import http.Http;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.*;

/**
 * Created by User on 2017/12/19.
 */
public class aaa {

    public static String sid = "6101609f77d30c7d017d07360f264326528448f7eee0e6179742176";
    public static String sid2 = "6100c04852051d4076d3cb038f688f0beaa5ca784e93322333336410";

    public static void main(String[] args) throws IOException, InterruptedException {
       /* String jsonString = getNumiid(15, sid);
        System.out.println(jsonString);

        String req = req(jsonString);
        System.out.println(req);


        String jsonString2 = getNumiid(16, sid2);
        System.out.println(jsonString2);

        String req2 = req(jsonString2);
        System.out.println(req2);*/

        List<Map> list = new ArrayList();

        ssss("select * from alibaba_cat a where a.parentId in ('10166','312','10165','311')", list);

        System.out.println(list.size());

        List<Data> result = new ArrayList<>();

        for (Map map : list) {
            CatData catData = new CatData(String.valueOf(map.get("catId")), (String) map.get("catName"));
            CatIdsFrom1688 catIdsFrom1688 = NumiidGet.requestMoblieCatIdsFrom1688ByCatData(catData);
            if (catIdsFrom1688.getNumiids().isEmpty()) {
                System.out.println(JSON.toJSONString(catData));
                continue;
            }
            int r = new Random().nextInt(catIdsFrom1688.getNumiids().size());

            Data data = new Data().setType(2).setNumIid(catIdsFrom1688.getNumiids().get(r)).setCid("0").setSid(sid);
            result.add(data);
        }

        System.out.println(JSON.toJSONString(result));

        req(JSON.toJSONString(result));

    }

    private static void ssss(String sql, List result) {

        List<Map> list1 = DB.exec(sql);

        for (Map map : list1) {
            Boolean isleaf = (Boolean) map.get("isLeaf");
            if (isleaf) {
                result.add(map);
            } else {
                Long catId = (Long) map.get("catId");
                ssss("select * from alibaba_cat a where a.parentId = " + catId, result);
            }
        }



    }


    private static String req(String jsonString) throws IOException {
        String resp = Http.sendPost("http://z.taovgo.com/ItemCarrierForDQ/itemCarry", "paramJson=" + jsonString, "UTF-8");

        return resp;
    }

    private static String getNumiid(int page, String sid) throws UnsupportedEncodingException, InterruptedException {
        int size = 500;
        int start = (page - 1) * size;

        List<Map> exec = DB.exec("select * from alibaba_cat where isleaf = 1 limit " + start + ", " + size);
        int count = exec.size();
        int donecount = 1;
        List result = new ArrayList();
        for (Map map : exec) {
            CatData catData = new CatData(String.valueOf(map.get("catId")), (String) map.get("catName"));
            CatIdsFrom1688 catIdsFrom1688 = NumiidGet.requestMoblieCatIdsFrom1688ByCatData(catData);
            if (catIdsFrom1688.getNumiids().isEmpty()) {
                System.out.println(JSON.toJSONString(catData));
                continue;
            }
            Data data = new Data().setType(2).setNumIid(catIdsFrom1688.getNumiids().get(0)).setCid("0").setSid(sid);
            result.add(data);
            System.out.println((donecount++) + "/" + count + " " + JSON.toJSONString(data));
        }
        String jsonString = JSON.toJSONString(result);
        return jsonString;
    }

    @org.junit.Test
    public void aaa() throws IOException {
        Long[] longs1 = new Long[]{1278447975L,
                522072322155L,
                559794963313L,
                521903458867L,
                43690560116L,
                557196804067L,
                38070881986L,
                525365967791L,
                556718209602L,
                560196319561L,
                42675535916L,
                542664563223L,
                39560804047L,
                551162008341L,
                35429490169L,
                1100473002L,
                411644653L,
                560443471705L,
                36235152844L,
                559841756967L,
                1154916552L,
                550182218005L,
                44359650232L,
                555377249625L,
                550452443294L,
                45326143450L,
                1285792845L,
                554206176595L,
                1259987166L,
                526997336112L,
                1170335601L,
                534005981454L,
                43864337512L,
                521688582603L,
                543356789774L,
                1226044879L,
                154323558L,
                545194622751L,
                41231955L,
                1010759329L,
                536859094181L,
                520341387417L,
                37016362123L,
                45187699316L,
                40648931190L,
                1219432264L,
                45215566914L,
                530520239416L,
                45215566914L,
                522135941979L,
                542853653447L,
                548465905323L,
                520738476307L,
                520905109355L,
                557053667510L,
                520729599595L,
                521180989735L,
                528585522880L,
                520701748661L,
                42310851705L,
                1121271099L,
                526443266402L,
                553461667474L,
                522561182977L,
                557919705942L,
                546657885154L,
                544192204273L,
                39899914559L,
                561056488153L,
                553203797581L,
                536936887163L,
                45492328306L,
                530662623705L,
                40947182641L,
                537767119336L,
                536478042190L,
                43968898822L,
                546684232805L,
                43446418090L,
                536947614608L,
                54260264L,
                43142089726L,
                545642248896L,
                524219617555L,
                37352055591L,
                541125091933L,
                561968054073L,
                526590644155L,
                1089482079L,
                542930430229L,
                623980305L,
                41707796256L,
                524610703075L,
                548446341319L,
                44273473574L,
                543658898625L,
                529697930624L,
                44273473574L,
                550881105291L,
                543134708039L,
                546566050192L,
                529042038961L,
                550925743435L,
                822487387L,
                537186298741L,
                541076046820L,
                44754010195L,
                529042038961L,
                535661889603L,
                529042038961L,
                552848805500L,
                528424952163L,
                560492609541L,
                908101162L,
                523362147395L,
                544000255718L,
                520139570168L,
                44960155226L,
                527226507790L,
                546837516058L,
                39458426832L,
                525062573307L,
                528186089445L,
                1262329676L,
                40049033020L,
                520441869444L,
                44960155226L,
                520139570168L,
                43331065348L,
                543974187775L,
                556775416107L,
                544431299897L,
                559364403467L,
                525151174760L,
                542558161829L,
                525932747502L,
                37477593591L,
                532601338203L,
                43193856774L,
                1225672382L,
                45406504403L,
                554213732400L,
                1243920981L,
                558436600840L,
                45406504403L,
                1214286625L,
                539968070023L,
                917402448L,
                544298521581L,
                544059020748L,
                543591795367L,
                529717449798L,
                531656439717L,
                545226697423L,
                627508991L,
                522956826584L,
                525879468809L,
                43495180398L,
                536409711159L,
                545865373134L,
                44255337061L,
                43073583675L,
                1230510323L,
                44036634447L,
                545058767339L,
                521739015584L,
                542530730693L,
                542263099324L,
                544059020748L,
                557203083351L,
                765644456L,
                561023379991L,
                37637296687L,
                521059282693L,
                549075367715L,
                536507552663L,
                42391306473L,
                543165368980L,
                41243942325L,
                553625120729L,
                43232734682L,
                40120186654L,
                41209970413L,
                809770117L,
                45278325819L,
                560031843636L,
                528528667435L,
                39458982465L,
                524201005536L,
                531861782178L,
                560465076493L,
                36303120699L,
                43530028111L,
                543895177199L,
                545714314127L,
                545113915563L,
                531596661765L,
                557245054112L,
                551018099442L,
                556897365608L,
                43516284179L,
                554935936216L,
                531181204049L,
                555258520356L,
                43861520040L,
                549920839014L,
                1200869341L,
                520938696418L,
                526587349652L,
                40312548508L,
                41399212712L,
                536946329116L,
                546295384645L,
                546295956841L,
                39283991604L,
                555782300123L,
                545991454672L,
                541393751701L,
                41472984307L,
                39283991604L,
                520850616515L,
                524136640273L,
                545027896816L,
                522880638873L,
                44196614606L,
                1174855585L,
                544988348541L,
                523314752972L,
                561263062701L,
                38887432002L,
                555421371172L,
                1212251333L,
                40357505977L,
                45314960741L,
                520822347661L,
                520962332820L,
                524001076688L,
                524090091837L,
                524885178925L,
                561171188745L,
                542804453081L,
                542303263953L,
                561869074442L,
                810944346L};

        Long[] longs = new Long[] {
                822487387L,
                537186298741L,
                541076046820L,
                44754010195L,
                529042038961L,
                535661889603L,
                529042038961L,
                552848805500L,
                528424952163L,
                560492609541L,
                908101162L,
                523362147395L,
                544000255718L,
                520139570168L,
                44960155226L,
                527226507790L,
                546837516058L,
                39458426832L,
                525062573307L,
                528186089445L,
                1262329676L,
                40049033020L,
                520441869444L,
                44960155226L,
                520139570168L,
                43331065348L,
                543974187775L,
                556775416107L,
                544431299897L,
                559364403467L,
                525151174760L,
                542558161829L,
                525932747502L,
                37477593591L,
                532601338203L,
                43193856774L,
                1225672382L,
                45406504403L,
                554213732400L,
                1243920981L,
                558436600840L,
                45406504403L,
                1214286625L,
                539968070023L,
                917402448L,
                544298521581L,
                544059020748L,
                543591795367L,
                529717449798L,
                531656439717L,
                545226697423L,
                627508991L,
                522956826584L,
                525879468809L,
                43495180398L,
                536409711159L,
                545865373134L,
                44255337061L,
                43073583675L,
                1230510323L,
                44036634447L,
                545058767339L,
                521739015584L,
                542530730693L,
                542263099324L,
                544059020748L,
                557203083351L,
                765644456L,
                561023379991L,
                37637296687L,
                521059282693L,
                549075367715L,
                536507552663L,
                42391306473L,
                543165368980L,
                41243942325L,
                553625120729L,
                43232734682L,
                40120186654L,
                41209970413L,
                809770117L,
                45278325819L,
                560031843636L,
                528528667435L,
                39458982465L,
                524201005536L,
                531861782178L,
                560465076493L,
                36303120699L,
                43530028111L,
                543895177199L,
                545714314127L,
                545113915563L,
                531596661765L,
                557245054112L,
                551018099442L,
                556897365608L,
                43516284179L,
                554935936216L,
                531181204049L,
                555258520356L,
                43861520040L,
                549920839014L,
                1200869341L,
                520938696418L,
                526587349652L,
                40312548508L,
                41399212712L,
                536946329116L,
                546295384645L,
                546295956841L,
                39283991604L,
                555782300123L,
                545991454672L,
                541393751701L,
                41472984307L,
                39283991604L,
                520850616515L,
                524136640273L,
                545027896816L,
                522880638873L,
                44196614606L,
                1174855585L,
                544988348541L,
                523314752972L,
                561263062701L,
                38887432002L,
                555421371172L,
                1212251333L,
                40357505977L,
                45314960741L,
                520822347661L,
                520962332820L,
                524001076688L,
                524090091837L,
                524885178925L,
                561171188745L,
                542804453081L,
                542303263953L,
                561869074442L,
                810944346L
        };

        List list= new ArrayList<>();
        for (Long l : longs) {
            Data data =new Data().setType(2).setNumIid(l.toString()).setCid("0").setSid("6100c04852051d4076d3cb038f688f0beaa5ca784e93322333336410");
            list.add(data);
        }

        System.out.println(JSON.toJSONString(list));

        String req = req(JSON.toJSONString(list));
        System.out.println(req);
    }



    static class Data  {
        String numIid;
        int type;
        String sid;
        String cid;

        public String getNumIid() {
            return numIid;
        }

        public Data setNumIid(String numIid) {
            this.numIid = numIid;
            return this;
        }

        public int getType() {
            return type;
        }

        public Data setType(int type) {
            this.type = type;
            return this;
        }

        public String getSid() {
            return sid;
        }

        public Data setSid(String sid) {
            this.sid = sid;
            return this;
        }

        public String getCid() {
            return cid;
        }

        public Data setCid(String cid) {
            this.cid = cid;
            return this;
        }
    }
}
